package com.company._class01;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestClass {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
        Person p=new Person();
        p.setName("张三");
        p.setAge(66);
//        p.work();


        Class c=Class.forName("com.company._class01.Person");
        Person p1=(Person)c.newInstance();

        //反射得到属性
        Field f=c.getDeclaredField("age");
        f.setAccessible(true);
        f.set(p1,20);
//        p1.work();


        //反射方式调用方法
        Method m=c.getDeclaredMethod("work",new Class[]{});
        m.invoke(p1);
    }
}
